import type { OpinionProgrammeBook, Opinion as PrismaOpinion } from '@fill/client';
import keyBy from 'lodash.keyby';
import { BOOK_TEXT, FILL_TABLE_COLLECTION, NODE_TYPE, OPINION_TEXT, PROGRAMME_TEXT, TABLE_COLLECTION } from '../../constants/default';
import type { fkyjxqb_ } from '../types/fkyj';
import type { w_zgfa } from '../types/zgfa';
import type { w_zgtz } from '../types/zgtz';
import type { IImportSchema, ILanhaiDataSchema, ILanhaiSpDef, ILanhaiSpNode, IPipe } from './interface';
import { Pipe } from './pipe';

export class Opinion extends Pipe implements IPipe {
  async import(data: ILanhaiDataSchema, _sp_def: ILanhaiSpDef, _sp_node: ILanhaiSpNode) {
    const opinions = await this.handleOpinion(data);
    // const programmes = await this.handleProgrameBook(data, sp_def, sp_node);

    const result = ([
      opinions,
      // programmes,
    ].filter(item => !!item && !!item.data.length)) as IImportSchema[];
    // console.log('-------->', opinions, programmes, result);

    return result;
  }

  async handleOpinion(data: ILanhaiDataSchema): Promise<IImportSchema | undefined> {
    if (!data.has(TABLE_COLLECTION.OPINION))
      return;
    // const opinions = keyBy(data.get(TABLE_COLLECTION.OPINION)?.data as fkyj_[], 'ID_');
    // const relations = keyBy(data.get(TABLE_COLLECTION.OPINION_RELATION)?.data as fkyjxqb_[], 'ID_');

    // const _data = Object.values(opinions).map((item) => {
    //   return {
    //     ...item,
    //     ...relations[item.ID_],
    //   };
    // });

    const _data = data.get(TABLE_COLLECTION.OPINION_RELATION)?.data as fkyjxqb_[];

    return {
      module: FILL_TABLE_COLLECTION.OPINION,
      main: true,
      text: [OPINION_TEXT],
      data: _data.map((item): PrismaOpinion => {
        return {
          uuid: item.ID_,
          slug: item.F_WTFL_,
          description: item.F_WTFM_,
          content: item.F_JTWT_,
          remark: item.F_BZ_ || '',
          created_at: item.CREATE_TIME_,
          updated_at: item.UPDATE_TIME_,
        };
      }),
    };
  }

  async handleProgrameBook(data: ILanhaiDataSchema, sp_def: ILanhaiSpDef, sp_node: ILanhaiSpNode) {
    if (!data.has(TABLE_COLLECTION.PROGRAMME))
      return;

    // 导入情况只重置处理状态
    const programmes = keyBy(data.get(TABLE_COLLECTION.PROGRAMME)?.data as w_zgfa[], 'F_FKYJ_ID_');
    const books = keyBy(data.get(TABLE_COLLECTION.BOOK)?.data as w_zgtz[], 'F_FKYJ_ID_');

    const _data = Object.values(programmes).map((item) => {
      return {
        ...item,
        ...books[item.F_FKYJ_ID_],
      };
    });

    return {
      module: FILL_TABLE_COLLECTION.PROGRAME_BOOK,
      main: true,
      text: [BOOK_TEXT, PROGRAMME_TEXT],
      data: _data.map((item): OpinionProgrammeBook => {
        console.log(item);

        // 查找当前反馈意见整改方案节点
        const programme_node = sp_node[NODE_TYPE.PROGRAMME][item.F_FKYJ_ID_];
        // 查找当前反馈意见整改台账节点
        const book_node = sp_node[NODE_TYPE.BOOK][item.F_FKYJ_ID_];

        return {
          uuid: item.F_FKYJ_ID_,
          department: item.F_ZRBM_,
          leader: item.F_ZRLD_,
          user: item.F_ZJZRR_,
          completed_at: item.F_FAWCSX_,
          is_rectification: item.F_SFZG_,
          proposal: item.F_XCJY_,
          measures: item.F_ZGCS_,
          rectification_status: item.F_ZGZT_,
          completion_remark: item.F_ZGWCQK_,
          payment_found: item.F_QJZJ_,
          other: item.F_QT_,
          other_organization: item.F_WZDZZ_,
          investigation: item.F_ZRZJ_,
          perfect: item.F_WSZD_,
          node: programme_node.F_DEF_ID_,
          next: programme_node.F_NEXT_,
          node_id: programmes[item.F_FKYJ_ID_].ID_,
          state: programme_node.F_STATE_,
          book_node: book_node.F_DEF_ID_,
          book_next: book_node.F_NEXT_,
          book_node_id: books[item.F_FKYJ_ID_].ID_,
          book_state: book_node.F_STATE_,
          book_completed_at: item.F_BGWCSX_,
          note: '',
          book_note: '',
          status: '',
          book_status: '',
          created_at: item.CREATE_TIME_,
          updated_at: item.UPDATE_TIME_,
        };
      }),
    };
  }

  async handleProgrammeRecord(_data: ILanhaiDataSchema) {
    //
  }

  async handleBookRecord() {

  }

  async export() {
    return {};
  }
}
